package com.zhixing.service.parse;

import com.zhixing.utils.JsonUtils;
import lombok.Getter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Swagger json 解析器
 *
 * @author zhixing
 */
public class SwaggerJsonStrDefinitionsParser implements SwaggerParser{

    /** swagger json 解析后的所有 map数据 **/
    @Getter
    private Map<String, Object>         allMapResult;

    /** swagger json 字符串 **/
    @Getter
    private String                      swaggerJsonStr;

    private SwaggerJsonStrDefinitionsParser(){}

    public static SwaggerJsonStrDefinitionsParser.Builder createBuild(){
        return  new SwaggerJsonStrDefinitionsParser.Builder();
    }

    public static class Builder{
        private SwaggerJsonStrDefinitionsParser parser;
        public Builder(){
            this.parser = new SwaggerJsonStrDefinitionsParser();
        }

        public SwaggerJsonStrDefinitionsParser.Builder swaggerJsonStr(String swaggerJsonStr){
            this.parser.swaggerJsonStr = swaggerJsonStr;
            return this;
        }

        public SwaggerJsonStrDefinitionsParser build(){
            return this.parser;
        }

    }

    @Override
    public SwaggerJsonStrDefinitionsParser parse() throws IOException {
        doParse();
        return this;
    }

    private void doParse() throws IOException {
        // swagger Json串转换成 map 对象
        allMapResult = JsonUtils.readValue(this.getSwaggerJsonStr(), HashMap.class);

    }
}
